﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Liolikas.GameBoard;

namespace Liolikas.GameObjects
{
    abstract class GameObject
    {
        public int hCoord { get; set; }
        public int wCoord { get; set; }

        public int lastHCoord { get; set; }
        public int lastWCoord { get; set; }

        protected int initHCoord;
        protected int initWCoord;
        protected List <Field> fields=new List<Field>();
       
        public String model;
        public ConsoleColor objectColor = ConsoleColor.White;
        public ConsoleColor objectBackgroundColor = ConsoleColor.Black;

        protected GameObject(int hCoord, int wCoord)     
        {
            this.initWCoord = wCoord;
            this.initHCoord = hCoord;
        }

        virtual public void MakeStep()
        {
            lastHCoord = hCoord;
            lastWCoord = wCoord;
            GetNewCoordinates();
            if (hCoord < 0 || hCoord >= Board.BOARD_HEIGHT) hCoord = lastHCoord;
            if (wCoord < 0 || wCoord + model.Length > Board.BOARD_WIDTH) wCoord = lastWCoord;
            if (lastHCoord != hCoord || lastWCoord != wCoord) UpdateCoordinates();
        }

        virtual protected void UpdateCoordinates()
        {
            foreach(Field field in fields)
                field.gameObject = null;
            fields = new List<Field>();

            for (int i = 0; i < model.Length; i++)
            {
                Field field = Board.GetBoard().GetField(hCoord, wCoord + i);
                this.fields.Add(field);
                if (field.gameObject != null)
                    Collide(field.gameObject);
                field.gameObject = this;
            }
            
            
        }

        virtual public void Collide(GameObject gameObj) { }
        virtual protected void Draw()
        {
            Console.Write(model);
        }
        

        abstract public void GetNewCoordinates();
    }
}
